﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 仿VS窗体.Base.Data;
using 仿VS窗体.Base.Util;

namespace 仿VS窗体
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public enum BaseWindowState { Normal, Minimized, Maximized, FullScreen, None }
        private BaseWindowState _baseWindowState = BaseWindowState.Normal;
        public BaseWindowState baseWindowState
        {
            get { return _baseWindowState; }
            set
            {
                _baseWindowState = value;
                if (value == BaseWindowState.Maximized || value == BaseWindowState.FullScreen) MaxPath.Data = Geometry.Parse(PathData.MaxToNormalPath);
                else MaxPath.Data = Geometry.Parse(PathData.MaxPath);
            }
        }



        public Point WindowPoint = new Point(100, 100);
        public Size WindowSize = new Size(1000, 600);

        public MainWindow()
        {
            InitializeComponent();
        }




        private bool canDragWindow = false;
        private void DragMoveWindow(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount > 1)
            {
                MaxWindow(sender, null);
            }
            canDragWindow = true;
        }

        private void MoveWindow(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && canDragWindow)
            {
                if (this.WindowState == WindowState.Maximized || (this.Width == SystemParameters.WorkArea.Width && this.Height == SystemParameters.WorkArea.Height))
                {
                    Grid grid = sender as Grid;
                    baseWindowState = 0;
                    double fracWidth = e.GetPosition(grid).X / grid.ActualWidth;
                    this.Width = WindowSize.Width;
                    this.Height = WindowSize.Height;
                    this.WindowState = WindowState.Normal;
                    this.Left = e.GetPosition(grid).X - grid.ActualWidth * fracWidth;
                    this.Top = e.GetPosition(grid).Y - grid.ActualHeight / 2;
                    this.OnLocationChanged(EventArgs.Empty);
                }

                this.DragMove();
            }
        }

        private void TopBorder_MouseLeave(object sender, MouseEventArgs e)
        {
            canDragWindow = false;
        }

        private void TopBorder_MouseUp(object sender, MouseButtonEventArgs e)
        {
            canDragWindow = false;
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.Width == SystemParameters.WorkArea.Width || this.Height == SystemParameters.WorkArea.Height)
            {
                this.ResizeMode = ResizeMode.NoResize;
            }
            else if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
                baseWindowState = BaseWindowState.Normal;
                MaxWindow(this, null);
            }
            else
            {
                this.ResizeMode = ResizeMode.CanResize;
            }
        }

        private void CloseWindow(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void MaxWindow(object sender, RoutedEventArgs e)
        {
            if (baseWindowState == 0)
            {
                //最大化
                baseWindowState = BaseWindowState.Maximized;
                WindowPoint = new Point(this.Left, this.Top);
                WindowSize = new Size(this.Width, this.Height);
                this.Width = SystemParameters.WorkArea.Width;
                this.Height = SystemParameters.WorkArea.Height;
                this.Top = SystemParameters.WorkArea.Top;
                this.Left = SystemParameters.WorkArea.Left;

            }
            else
            {
                baseWindowState = BaseWindowState.Normal;
                this.Left = WindowPoint.X;
                this.Width = WindowSize.Width;
                this.Top = WindowPoint.Y;
                this.Height = WindowSize.Height;

            }
            this.WindowState = WindowState.Normal;
            this.OnLocationChanged(EventArgs.Empty);
        }

        private void MinWindow(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void SwitchLanguage(object sender, RoutedEventArgs e)
        {
            WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture =
                System.Globalization.CultureInfo.GetCultureInfo(((MenuItem)sender).Tag.ToString());
            Console.WriteLine(LangHelper.GetLocalizedString("Title"));
        }

        private void SwitchTheme(object sender, RoutedEventArgs e)
        {
            string theme = ((MenuItem)sender).Tag.ToString();
            Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary()
            {
                Source = new Uri("Base/Style/Color/" + theme + ".xaml", UriKind.Relative)
            };
            TitleGrid.ContextMenu.UpdateLayout();
        }
    }
}
